#include "logindialog.h"
#include "ui_widget.h"
#include <QLabel>
#include <QTextLine>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>

#include <textinput.h>
#include <QIcon>
#include <QComboBox>
#include <QDebug>
#include <QtSql/QSqlDatabase>
#include <QMessageBox>
#include <QtSql/QSqlError>
#include <QSqlQuery>
#include <QStringList>
#include <db.h>
#include <emp_mainwindow.h>
#include <QTimer>
#include <registerdialog.h>
#include <databasetask.h>
#include <QThreadPool>
#include <vector>

LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    /* 窗口标题和图片 */
    this->setWindowTitle("智慧医疗管理系统");
    QPixmap pixmap;
    pixmap.load(":/images/TitleIcon.png");
    QIcon icon;
    icon.addPixmap(pixmap);
    this->setWindowIcon(icon);
    /* 窗口标题和图片 */

    ui->labelTitle->setLabelName("智慧医疗系统登录");
    ui->labelTitle->showLabelName();
    createCombox();
    createTextInput();
    createButton();

    db = DB::getInstance();

}
LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::createButton()
{
    buttot_1 = new QPushButton();
    buttot_2 = new QPushButton();

    buttot_1->setText("登录");
    buttot_2->setText("注册");

    buttonLayout = new QHBoxLayout(ui->buttonWidget);
    buttonLayout->addStretch();
    buttonLayout->addWidget(buttot_1);
    buttonLayout->addWidget(buttot_2);
    buttonLayout->addStretch();

    connect(buttot_1,&QPushButton::clicked,this,&LoginDialog::updateLoginUserInfo);
    connect(buttot_2,&QPushButton::clicked,
            [=]()
    {
        emit this->showToRegisterDialog();
    });
}

void LoginDialog::createTextInput()
{
    ui->textInput_1->setLabelName("用户名");
    ui->textInput_1->setPlaceholderText("请输入用户名");
    ui->textInput_1->setTextInputModel("Normal");
    ui->textInput_1->showTextInput();

    ui->textInput_2->setLabelName("密码");
    ui->textInput_2->setPlaceholderText("请输入密码");
    ui->textInput_2->setTextInputModel("Password");
    ui->textInput_2->showTextInput();
}

void LoginDialog::createCombox()
{
    // 创建 QComboBox 对象
    comboBox = new QComboBox(ui->role);
    // 添加选项
    comboBox->addItem("患者");
    comboBox->addItem("医生");
    comboBox->addItem("管理员");
    roleLabel = new QLabel(ui->role);
    roleLabel->setText("角色选择");
    roleLayout = new QHBoxLayout(ui->role);
    roleLayout->addStretch();
    roleLayout->addWidget(roleLabel);
    roleLayout->addWidget(comboBox);
    roleLayout->addStretch();
}

void LoginDialog::updateLoginUserInfo()
{
    this->role = this->comboBox->currentText();
    this->username = ui->textInput_1->getTextInput();
    this->password = ui->textInput_2->getTextInput();
    if(ui->textInput_1->getTextInput().size() == 0)
    {
        QMessageBox::information(this, "输入内容", "登录失败,请填写正确用户名");
        return;
    }
    if(ui->textInput_2->getTextInput().size() == 0)
    {
        QMessageBox::information(this, "输入内容", "登录失败,请填写正确密码");
        return;
    }
    QString quires = QString("SELECT * FROM tms WHERE tms_gg = '%1' AND tms_user = '%2' AND tms_password = '%3'").arg(role, username, password);
    QVariantMap params = {
        {"tms_gg", role},
        {"tms_user", username},
        {"tms_password", password}
    };
    DatabaseTask* task = new DatabaseTask(quires,params,"登录");
    connect(task,&DatabaseTask::finished,this,&LoginDialog::handleResults);
    connect(task, &DatabaseTask::error,this, &LoginDialog::handleError);
    QThreadPool::globalInstance()->start(task);
}

void LoginDialog::handleResults(const QStringList& results)
{
    // 处理查询结果
    if (results.size() == 0) {
            // 没有查询到结果
            QMessageBox::information(this, "登录结果", "登录失败,请检查信息是否填写正确");
        } else if (results.size() == 1) {
            // 查询到一个结果
            QString result = results.at(0);
            qDebug() << "User found:" << result;
            // 返回 QDialog::Accepted 表示登录成功
            this->accept();
    }
}

void LoginDialog::handleError(const QString& errorMessage)
{
    qDebug() << "Database error:" << errorMessage;
}
